"""
@author: yeshengtao
@file: 面向对象三乐系统.py
@time: 2023/4/13 15:55
"""
#购物系统类：
# 实例属性
#   商品信息
#   系统说明信息
#   当前用户 -- 多个
#方法：
    # 登入注册
    # 查看商品
    # 添加购物车
    # 结算

# 用户类：
#     用户名
#     密码
#     余额
#     购物车
# 方法
import json
goods={"#F00001":{"name":"apple","price":1.4},
            "#F00002":{"name":"banana","price":2},
            "#F00003":{"name":"梨子","price":4},
            "#F00004":{"name":"桃子","price":5},
            "#F00005":{"name":"蟹黄面","price":30},
            "#F00006":{"name":"部队火锅","price":25}
            }
drugs = {
    "M0001":{"name":"三九胃泰","price":17},
    "M0002":{"name":"珍视明滴眼液","price":35}
}
class User:
    def __init__(self,name,passwd,init_amount):
        self.name = name
        self.passwd = passwd
        self.init_amount = init_amount
        self.cart = {}

class ShoppingSystem:
    def __init__(self,goods_info,sys_info):
        self.goods_info = goods_info
        self.sys_info = sys_info
        fp = open("cst.txt","w+")
        rd = fp.read()
        print(len(rd))
        if len(rd) == 0:
            p = json.dumps({})
            fp.write(p)
            self.userdict = {}
        else:
            str = json.loads(rd)
            self.userdict = str
    def register(self,name,passwd,Initial_amount):
        # 创建一个新用户,实例化一个user类
        if name not in self.userdict:
            user = User(name, passwd, Initial_amount)
            self.userdict[name]={"passwd":user.passwd,"amount":user.init_amount}
            fp = open("cst.txt","w")
            wr = json.dumps(self.userdict)
            fp.write(wr)
            fp.close()
        else:
            print("用户名已被占用")
    def login(self,name,passwd):
        print("开始登入")
        if name in self.userdict:
            print(self.userdict[name]["密码"])
            if self.userdict[name]["密码"] == passwd:
                print("登入成功")
                return  1
            else:
                print("密码错误，请重新输入")
                return 0
        else:
            print("用户不存在，请选择：\n0.重新登入\n2.注册\n3.退出\n")
            num = int(input())
            return num

lbx_drugstore = ShoppingSystem(drugs,"老百姓大药房")
dzy_shop = ShoppingSystem(goods,"东之源生活超市")

system = [lbx_drugstore,dzy_shop]

while 1:
    print("当前已有购物系统如下")
    for i,v in enumerate(system):
        print(f"{i}.{v.sys_info}")
    c1 = input("请输入你的选择，按q退出\n")
    if c1 == "q":
        break
    if c1.isdigit() and int(c1)<len(system):
        print(f"欢迎进入{system[int(c1)].sys_info}".center(40,"*"))
        c_system = system[i]
        while 1:
            c2 = input("1.注册\n2.登入\n3.按q退出\n")
            if c2 == "q":
                break
            if c2 == "1":
                name,passwd,amount = input("请输入名字，密码，初始金额（空白分割）").split()
                c_system.register(name,passwd,amount)
                print(c_system.userdict)
            elif c2 == "2":
                num = 0
                user = ""
                while num == 0:
                    name,passwd = input("请输入名字，密码(使用空格分割)").split()
                    num = c_system.login(name,passwd)
                    print(num)
                    if num == 2:
                        name, passwd, amount = input("请输入名字，密码，初始金额（空白分割）").split()
                        c_system.register(name, passwd, amount)
                if num == 1:
                    while 1:
                        num2 = input("请选择：\n1.购物\n2.查看购物车\n3.退出\n")
                        if num2 == "1":
                            pass
                        elif num2 == "2":
                            pass
                        else:
                            break
                else:
                    break
            else:
                print("输入有误")
        pass
    else:
        print("输入有误请重新输入")
